package com.kobeliu.mq;

import com.google.gson.Gson;
import com.kobeliu.api.mq.DelayIndustryConfig;
import com.kobeliu.api.mq.RabbitMQSMSConfig;
import com.kobeliu.pojo.mq.SMSContentQO;
import com.kobeliu.service.ArticleService;
import com.kobeliu.service.impl.ArticleServiceImpl;
import com.kobeliu.utils.SMSUtils;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.awt.event.WindowFocusListener;

@Slf4j
@Component
public class DelayExchangeConsumer {

    @Autowired
    private SMSUtils smsUtils;

    @Autowired
    private ArticleService articleService;

    //监听器 监听绑定的队

    @RabbitListener(queues = {DelayIndustryConfig.QUEUE_DELAY_REFRESH})
    public void watchQueue(Message message, Channel channel) throws Exception {

//        String receivedRoutingKey = message.getMessageProperties().getReceivedRoutingKey();

//        String msg = message.getMessageProperties().getReceivedRoutingKey();
//
//        //message.getBody() 是一个byte数组
//        String msg1 = new String(message.getBody());

        try{
            String receivedRoutingKey = message.getMessageProperties().getReceivedRoutingKey();
            if(receivedRoutingKey.equalsIgnoreCase(DelayIndustryConfig.DELAY_REFRESH_INDUSTRY)){
                String content = message.getBody().toString();


            }
            if(receivedRoutingKey.equalsIgnoreCase(ArticleServiceImpl.DELAY_REFRESH_ARTICLE)){
                //定期发布文章
                String articleId = new String(message.getBody());
                log.info("===========================");
                log.info("准备发布的文章ID为{}",articleId);
                log.info("===========================");
                articleService.publishArticle(articleId);

            }
            // 消息投递标签
            // 批量确认所有消费真获得的消息
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),true);
        }catch (Exception e){
            e.printStackTrace();
            // 消息投递标签
            // 批量确认所有消费真获得的消息
            // requeue 重回队列的策略 true 重回 false丢弃
            channel.basicNack(message.getMessageProperties().getDeliveryTag(),true,true);
        }

    }
}
